﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using OConf.Model.Common;
using System.Linq;

namespace OConf.API.Filter
{
    public class RequestParamActionFilter : ActionFilterAttribute
    {
        public RequestParamActionFilter()
        {
            Order = 0;
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            ReturnData<object> result = new ReturnData<object>();
            if (!context.ModelState.IsValid)
            {
                var errorMessage = string.Join(";", context.ModelState.Keys.SelectMany(key => context.ModelState[key].Errors.Select(x => x.ErrorMessage)).ToList());
                result.Code = (int)VabStateCode.StatusCode.请求参数错误;
                result.Msg = errorMessage;
                result.Data = null;
                context.Result = new OkObjectResult(result);
            }
        }
    }
}